package com.changgou.goods.controller;

import com.changgou.common.constant.StatusCode;
import com.changgou.common.vo.Result;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.goods.service.SpuService;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.itheima.com
 * @Description:
 * @Date www.itheima.com
 *****/

@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
         * 还原被删除商品
         * @param id 商品spu id
         * @return 操作结果
         */
    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable Long id){
        spuService.restore(id);
        return new Result("还原成功！");
    }


    /**
         * 逻辑删除商品
         * @param id 商品spu id
         * @return 操作结果
         */
    @DeleteMapping("/logic/delete/{id}")
    public Result logicDelete(@PathVariable Long id){
        spuService.logicDelete(id);
        return new Result("删除成功！");
    }

    /**
         * 下架商品
         * @param id 商品spu id
         * @return 操作结果
         */
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable("id") Long id){
        spuService.pull(id);
        return new Result("下架成功！");
    }

    /**
     * 批量上架商品
     * @param ids 商品spu id数组；在postman中请求体类似设置为：[1,2,3]
     * @return 操作结果
     */
    @PutMapping("/put/many")
    public Result put(@RequestBody Long[] ids){
        Integer count = spuService.putMany(ids);
        return new Result("批量上架"+count+"个商品成功！");
    }

    /**
     * 上架商品
     * @param id
     * @return
     */
    @PutMapping("/put/{id}")
    public Result put(@PathVariable("id") Long id){
        spuService.put(id);
        return new Result("上架成功！");
    }



    /**
     * 审核商品
     * @param id
     * @return
     */
    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable("audit") Long id){
        spuService.audit(id);
        return new Result("审核成功！");
    }

    /**
     * 根据商品spu id 查询商品（spu+sku列表）
     * @param spuId
     * @return
     */
    @GetMapping("/goods/{spuId}")
    public Result findGoodsBySpuId(@PathVariable("spuId") Long spuId){
        Goods goods=spuService.findGoodsBySpuId(spuId);
        return new Result("查询成功！",goods);
    }

    /**
     * 保存商品spu和sku列表
     * @param goods
     * @return
     */
    @PostMapping("/save")
    public Result saveGoods(@RequestBody Goods goods){
        spuService.saveGoods(goods);
        return new Result("添加成功！");
    }

    /***
     * Spu分页条件搜索实现
     * @param spu: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Spu>> findPage(@RequestBody(required = false)  Spu spu, @PathVariable  int page, @PathVariable  int size){
        //调用SpuService实现分页条件查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(spu, page, size);
        return new Result<PageInfo<Spu>>("查询成功！", pageInfo);
    }

    /***
     * Spu分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Spu>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SpuService实现分页查询Spu
        PageInfo<Spu> pageInfo = spuService.findPage(page, size);
        return new Result<PageInfo<Spu>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param spu 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Spu>> findList(@RequestBody(required = false)  Spu spu){
        //调用SpuService实现条件查询Spu
        List<Spu> list = spuService.findList(spu);
        return new Result<List<Spu>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SpuService实现根据主键删除
        spuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Spu数据
     * @param spu 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody  Spu spu){
        //调用SpuService实现修改Spu
        spuService.update(spu);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Spu数据
     * @param spu 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody  Spu spu){
        //调用SpuService实现添加Spu
        spuService.add(spu);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Spu数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Spu> findById(@PathVariable Long id){
        //调用SpuService实现根据主键查询Spu
        Spu spu = spuService.findById(id);
        return new Result<Spu>("查询成功！", spu);
    }

    /***
     * 查询Spu全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Spu>> findAll(){
        //调用SpuService实现查询所有Spu
        List<Spu> list = spuService.findAll();
        return new Result<List<Spu>>("查询成功！", list);
    }
}
